package org.example.apply.controller;

import com.bw.springboot.starter.upload.model.Chunk;
import com.bw.springboot.starter.upload.service.FileService;
import com.bw.springboot.starter.upload.util.FileResult;
import lombok.extern.slf4j.Slf4j;
import org.example.apply.utils.ResultJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * 文件上传
 */

@Slf4j
@RestController
@RequestMapping("/apply")
public class FileUploadController {

    @Autowired
    private FileService fileService;

    @RequestMapping("postFileUpload")
    public ResultJson postFileUpload(@ModelAttribute Chunk chunk) throws InterruptedException {
        fileService.postFileUpload(chunk);
        if (chunk.getTotalChunks().equals(chunk.getChunkNumber())) {
            TimeUnit.SECONDS.sleep(2);
            FileResult fileResult = fileService.mergeFile(chunk);
            log.info("文件上传成功");
            return ResultJson.success(fileResult.getFileUrl());
        }
        return ResultJson.success();
    }
}
